package com.example.wxchatcotents;

import static de.robv.android.xposed.XposedHelpers.callMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.database.Cursor;
import android.text.TextUtils;

public class WechatMainDBHelper {

	private Object SQLDB;

	private HashMap<String, String> nicknameCache;

	public WechatMainDBHelper(Object dbObject) {
		SQLDB = dbObject;
		nicknameCache = new HashMap<>();
	}

	public Cursor getLastMsg() {
		return rawQuery("SELECT * FROM message order by msgId desc limit 0,1");
	}

	public Cursor rawQuery(String query) {
		return rawQuery(query, null);
	}

	public Cursor rawQuery(String query, String[] args) {
		return (Cursor) callMethod(SQLDB, "rawQuery", query, args);
	}

	public String getNickname(String username) {
		if (nicknameCache.containsKey(username)) {
			return nicknameCache.get(username);
		}

		Cursor cursor = getContact(username);
		if (cursor == null || !cursor.moveToFirst())
			return username;

		String name = cursor.getString(cursor.getColumnIndex("conRemark"));
		if (TextUtils.isEmpty(name)) {
			name = cursor.getString(cursor.getColumnIndex("nickname"));
		}
		name = name.trim();
		cursor.close();
		nicknameCache.put(username, name);
		return name;
	}

	private Cursor getContact(String username) {
		String query = "SELECT * FROM rcontact WHERE username = ?";
		return rawQuery(query, new String[] { username });
	}

	public long getContactCnt() {
		String query = "SELECT count(*) FROM contact ";
		Cursor cursor = rawQuery(query, null);
		cursor.moveToFirst();
		return cursor.getLong(0);
	}

	public Set<String> getAllTable() {
		Set<String> tables = new HashSet<String>();
		String query = "SELECT name FROM sqlite_master WHERE type='table' order by name";
		Cursor cursor = rawQuery(query, null);
		while (cursor.moveToNext()) {
			tables.add(cursor.getString(0));
		}
		return tables;
	}

	public List<List<String>> getAllData(String table) {
		String query = "SELECT * FROM " + table;
		Cursor cursor = rawQuery(query, null);
		List<List<String>> data = new ArrayList<List<String>>();

		List<String> cols = new ArrayList<String>();
		int cols_len = cursor.getColumnCount();
		for (int i = 0; i < cols_len; i++) {
			cols.add(cursor.getColumnName(i));
		}
		data.add(cols);

		while (cursor.moveToNext()) {
			List<String> row = new ArrayList<String>();
			for (int j = 0; j < cols_len; j++) {
				int type = cursor.getType(j);
				switch (type) {
				case 1:// int
					row.add(cursor.getInt(j)+"");
					break;
				case 2:// float
					row.add(cursor.getFloat(j)+"");
					break;
				case 4:// blob
					row.add("blob数据");
					break;
				default:
					row.add(cursor.getString(j));
					break;
				}
			}
			data.add(row);
		}
		return data;
	}
	
	public List<List<String>> getAllContactWithoutChatRoom(){
		String query = "SELECT * FROM  rcontact WHERE type&1!=0  and type&32=0 and type&8=0 and verifyFlag&8=0 and (username not like '%@%' or (type&1!=0 and type&8=0 and username like '%@talkroom'))";
		Cursor cursor = rawQuery(query, null);
		List<List<String>> data = new ArrayList<List<String>>();

		List<String> cols = new ArrayList<String>();
		int cols_len = cursor.getColumnCount();
		for (int i = 0; i < cols_len; i++) {
			cols.add(cursor.getColumnName(i));
		}
		data.add(cols);

		while (cursor.moveToNext()) {
			List<String> row = new ArrayList<String>();
			for (int j = 0; j < cols_len; j++) {
				int type = cursor.getType(j);
				switch (type) {
				case 1:// int
					row.add(cursor.getInt(j)+"");
					break;
				case 2:// float
					row.add(cursor.getFloat(j)+"");
					break;
				case 4:// blob
					row.add("blob数据");
					break;
				default:
					row.add(cursor.getString(j));
					break;
				}
			}
			data.add(row);
		}
		return data;
	}
	
	
	
	private String getChatroomName(String username) {
		String name = getNickname(username);
		if (!TextUtils.isEmpty(name))
			return name;

		String query = "SELECT * FROM chatroom WHERE chatroomname = ?";
		Cursor cursor = rawQuery(query, new String[] { username });
		if (cursor == null || !cursor.moveToFirst())
			return null;

		name = cursor.getString(cursor.getColumnIndex("displayname"));
		cursor.close();
		return name;
	}

}
